package com.jintoufs.web.action.finance;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.web.action.common.JxlsHandler;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/divided")
public class DividedAction extends SupportAction{
    @Resource
    private DividedService dividedService;

    @RequestMapping("/list")
    public String list(Model model, String order_id , String mobile_phone, String startDate, String endDate){
        model.addAttribute("order_id",order_id);
        model.addAttribute("mobile_phone",mobile_phone);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        return "divided/list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters,
                            String order_id , String mobile_phone, String startDate, String endDate) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }

        if(StringUtils.isNotEmpty(order_id)){
            QueryRule role = new QueryRule();
            role.setField("d.order_id");
            role.setOp("cn");
            role.setData(order_id);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(mobile_phone)){
            QueryRule role = new QueryRule();
            role.setField("u.mobile_phone");
            role.setOp("cn");
            role.setData(mobile_phone);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(startDate)){
            QueryRule role = new QueryRule();
            role.setField("d.createtime");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(endDate)){
            QueryRule role = new QueryRule();
            role.setField("d.createtime");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = dividedService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }
    @RequestMapping("download")
    @ResponseBody
    public void download(HttpServletRequest request, HttpServletResponse response, String sidx, String sord, String filters,
                         String order_id , String mobile_phone, String startDate, String endDate) {

        QueryFilters filters_;
        PageHelper.startPage(1, 1000);
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }

        if(StringUtils.isNotEmpty(order_id)){
            QueryRule role = new QueryRule();
            role.setField("d.order_id");
            role.setOp("cn");
            role.setData(order_id);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(mobile_phone)){
            QueryRule role = new QueryRule();
            role.setField("u.mobile_phone");
            role.setOp("cn");
            role.setData(mobile_phone);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(startDate)){
            QueryRule role = new QueryRule();
            role.setField("d.createtime");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if(StringUtils.isNotEmpty(endDate)){
            QueryRule role = new QueryRule();
            role.setField("d.createtime");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }

        filters_.setSidx(sidx);
        filters_.setSord(sord);

        List<Map<String,Object>> list = dividedService.getByFilter(filters_);
        try {
            JxlsHandler.report(request,response,"订单分润流水-下载模板.xlsx",list);
        }catch (IOException e){

        }
    }


}
